package com.projects.xenia.documents_activities;

import java.util.Calendar;
import java.util.Date;

import com.projects.xenia.MainCustomersListActivity;
import com.projects.xenia.R;
import com.projects.xenia.documents_classes.Documents_Task;
import com.projects.xenia.global_modules.MainGlobalModule;
import com.projects.xenia.ref_classes.CustomersRefItem;
import com.projects.xenia.ref_classes.CustomersRefObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;

import android.widget.ImageView;
import android.widget.ListView;



public class Activity_Task extends Activity {
	
	private final Context Ctx = this;
	private Documents_Task DocumentClass;
	
	private ImageView iv_backward;
	private ImageView iv_add;
	
	private Button btn_datetarget;
	private Button btn_status;
	private Button btn_customer;
	
	private ListView lv_tasks;
	
	private final int IDR_CustomerSelect = 0;
	private final int IDR_TaskAdd = 1;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);            
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
			
		setContentView(R.layout.task_form);
			
		Bundle DocExtras = getIntent().getExtras();
			
		DocumentClass = (Documents_Task) getLastNonConfigurationInstance();
		
		if (DocumentClass == null) {			
			DocumentClass = new Documents_Task ();
			if (DocExtras!=null) {
				if (DocExtras.getString("DocId")!=null) {
					DocumentClass.OpenDoc(DocExtras.getString("DocId"));
					}
				else {
					DocumentClass.NewDoc();
					DocumentClass.Head.customer = new CustomersRefObject(DocExtras.getString("CustomerId")).RefItem;
					}
			}	
			else {
				DocumentClass.NewDoc();
			}			
		}
		
				
		iv_backward = (ImageView)findViewById(R.id._backward);
		iv_backward.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				CloseDocument ();
			}
		});
		
		iv_add = (ImageView)findViewById(R.id._add);
		iv_add.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				
				Intent task_add = new Intent();
				task_add.setClass(Ctx, Activity_TaskAdd.class);
				startActivityForResult(task_add, IDR_TaskAdd);
				
			}
		});
		 
		
		btn_datetarget = (Button)findViewById(R.id.task_TargetDate);
		btn_datetarget.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {				
				Calendar C = Calendar.getInstance();
				C.setTime(DocumentClass.Head.date_target);				
				DatePickerDialog dd = new DatePickerDialog(Ctx, new DatePickerDialog.OnDateSetListener() {					
					@Override
					public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {						
						Calendar C = Calendar.getInstance();
						C.set(year, monthOfYear, dayOfMonth);
						Date TargetDate = C.getTime();
						btn_datetarget.setText(MainGlobalModule.fdf.format(C.getTime()));
						btn_datetarget.setTag(TargetDate);
						DocumentClass.Head.date_target = TargetDate;						
					}
				}, C.get(Calendar.YEAR), C.get(Calendar.MONTH), C.get(Calendar.DAY_OF_MONTH));
				dd.show();
			}
		});		
		btn_datetarget.setText(MainGlobalModule.fdf.format(DocumentClass.Head.date_target));
		btn_datetarget.setTag(DocumentClass.Head.date_target);
		
		
		btn_status = (Button)findViewById(R.id.task_Status);
		btn_status.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
		
				
			}
		});		
		
		btn_customer = (Button)findViewById(R.id.task_Customer);
		btn_customer.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {				
				Intent CustomerSelectintent = new Intent();
				CustomerSelectintent.setClass(Ctx,MainCustomersListActivity.class);
				
				if (DocumentClass.Head.customer!=null) {
					CustomerSelectintent.putExtra("customer_id", DocumentClass.Head.customer.id);
					CustomerSelectintent.putExtra("customer_descr",DocumentClass.Head.customer.descr);	
				}
				else {
					CustomerSelectintent.putExtra("customer_id", "");
					CustomerSelectintent.putExtra("customer_descr","");
				}
				
				startActivityForResult (CustomerSelectintent,IDR_CustomerSelect); 
				 
			}
		});
		
		RefreshCustomer (DocumentClass.Head.customer);		
		
		lv_tasks = (ListView)findViewById(R.id.task_List);		
		
	}
	
	public void RefreshCustomer (CustomersRefItem _customer) {
		
		if (_customer!=null) {
			RefreshCustomer (_customer.id);
		}
		
	}
	
	public void RefreshCustomer (String customer_id) {		
				
		if (customer_id!=null) {
			CustomersRefObject _customerobj = new CustomersRefObject (customer_id);
			btn_customer.setText(_customerobj.GetCustomersDescrLong());
			btn_customer.setTag(_customerobj.RefItem);
			DocumentClass.Head.customer = _customerobj.RefItem;
		}
		else 
		{
			btn_customer.setText("");
			btn_customer.setTag(null);
		}
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {	
		super.onActivityResult(requestCode, resultCode, data);
		
		switch(requestCode) { 
	    case IDR_CustomerSelect:
	    	if (resultCode==RESULT_OK) {
	    		RefreshCustomer (data.getStringExtra("customer_id"));	
	    	}
	    	break;
	    }		
		
	}
	
	 private void CloseDocument () {
	    	
			AlertDialog.Builder builder = new AlertDialog.Builder(this);		
			builder.setTitle("Закрытие документа");
			builder.setMessage("Сохранить изменения в документе ?");
			builder.setIcon(R.drawable.ic_achtung);
			
			//if ((lv_tasks.geti)>0) {
				builder.setPositiveButton("Да", new DialogInterface.OnClickListener() {    				    
					public void onClick(DialogInterface dialog, int which) {
						FinishActivity ();
				        dialog.dismiss();
				    }
				});
			//}		
			
			builder.setNegativeButton("Нет", new DialogInterface.OnClickListener() {
			    @Override
			    public void onClick(DialogInterface dialog, int which) {
			    	
			    	if (DocumentClass.BackupIsExists()) {
			    		DocumentClass.RestoreDocument();
			    		finish();
			    	}
			    	else {
			    		DocumentClass.Head.customer = null;
			    		FinishActivity ();
			    	}		    	
			        dialog.dismiss();
			    }
			});

			builder.setNeutralButton("Отмена",new DialogInterface.OnClickListener() {			
				@Override
				public void onClick(DialogInterface dialog, int which) {
					return;
				}
			}); 
			
			Dialog alert = builder.create();
			alert.show();    	
			
	    }
	 
	   public void FinishActivity () {  
	    	
	    	DocumentClass.Head.state = 1;
	    	DocumentClass.Head.verstamp++;
	    	DocumentClass.SaveDoc();
	    	DocumentClass.FinishDoc ();
	    	finish();
	    	
	    }
}
